package site.wlwsjsx.iot_backend.service.impl;

import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import site.wlwsjsx.iot_backend.entity.WorkstationInfo;
import site.wlwsjsx.iot_backend.mapper.WorkstationInfoMapper;
import site.wlwsjsx.iot_backend.service.WorkStationService;

import java.time.LocalDateTime;

@Service
public class WorkStationServiceImpl implements WorkStationService {
    private static final Logger logger = LoggerFactory.getLogger(WorkStationServiceImpl.class);
    @Resource
    private WorkstationInfoMapper workstationInfoMapper;
    private final RestTemplate restTemplate = new RestTemplate();
    private final static String IP = "192.168.137.118";

    @Override
    public void setPowerStatusByWorStaCode(String workStationCode, int powerStatus) {
        logger.info("开始设置工作站电源状态 - 工作站代码: {}, 电源状态: {}", workStationCode, powerStatus);
        try {
            WorkstationInfo workstationInfo = workstationInfoMapper.selectByCode(workStationCode);
            if (workstationInfo.getCurUserId() != null) {

            }
            String url = "http://" + IP + "/power?status=" + powerStatus;
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
            if (response.getStatusCode() == HttpStatus.OK) {
                workstationInfo.setPowerStatus(powerStatus);
                workstationInfo.setModifyTime(LocalDateTime.now());
                if (powerStatus == 0) {
                    workstationInfo.setLightStatus(0);
                    workstationInfo.setRelayNum1Status(0);
                    workstationInfo.setRelayNum2Status(0);
                }
                workstationInfoMapper.updateById(workstationInfo.getId(), workstationInfo);
                logger.info("设置工作站电源状态成功 - 工作站代码: {}, 响应: {}", workStationCode, response.getBody());
            } else {
                logger.error("设置工作站电源状态失败 - 工作站代码: {}, HTTP状态码: {}",
                        workStationCode, response.getStatusCode());
            }
        } catch (Exception e) {
            logger.error("设置工作站电源状态异常 - 工作站代码: {}, 错误信息: {}",
                    workStationCode, e.getMessage(), e);
        }
    }

    @Override
    public void setRelayNum1StatusByWorStaCode(String workstationCode, int relayNum1Status) {
        logger.info("开始设置工作站继电器1状态 - 工作站代码: {}, 继电器1状态: {}", workstationCode, relayNum1Status);
        try {
            String url = "http://" + IP + "/relay1?status=" + relayNum1Status;
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

            if (response.getStatusCode() == HttpStatus.OK) {
                WorkstationInfo workstationInfo = workstationInfoMapper.selectByCode(workstationCode);
                workstationInfo.setRelayNum1Status(relayNum1Status);
                workstationInfo.setModifyTime(LocalDateTime.now());
                if (workstationInfo.getPowerStatus() == 0) {
                    workstationInfo.setPowerStatus(1);
                }
                workstationInfoMapper.updateById(workstationInfo.getId(), workstationInfo);
                logger.info("设置工作站继电器1状态成功 - 工作站代码: {}, 响应: {}", workstationCode, response.getBody());
            } else {
                logger.error("设置工作站继电器1状态失败 - 工作站代码: {}, HTTP状态码: {}",
                        workstationCode, response.getStatusCode());
            }
        } catch (Exception e) {
            logger.error("设置工作站继电器1状态异常 - 工作站代码: {}, 错误信息: {}",
                    workstationCode, e.getMessage(), e);
        }
    }

    @Override
    public void setRelayNum2StatusByWorStaCode(String workstationCode, int relayNum2Status) {
        logger.info("开始设置工作站继电器2状态 - 工作站代码: {}, 继电器2状态: {}", workstationCode, relayNum2Status);
        try {
            String url = "http://" + IP + "/relay2?status=" + relayNum2Status;
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

            if (response.getStatusCode() == HttpStatus.OK) {
                WorkstationInfo workstationInfo = workstationInfoMapper.selectByCode(workstationCode);
                workstationInfo.setRelayNum2Status(relayNum2Status);
                workstationInfo.setModifyTime(LocalDateTime.now());
                if (workstationInfo.getPowerStatus() == 0) {
                    workstationInfo.setPowerStatus(1);
                }
                workstationInfoMapper.updateById(workstationInfo.getId(), workstationInfo);
                logger.info("设置工作站继电器2状态成功 - 工作站代码: {}, 响应: {}", workstationCode, response.getBody());
            } else {
                logger.error("设置工作站继电器2状态失败 - 工作站代码: {}, HTTP状态码: {}",
                        workstationCode, response.getStatusCode());
            }
        } catch (Exception e) {
            logger.error("设置工作站继电器2状态异常 - 工作站代码: {}, 错误信息: {}",
                    workstationCode, e.getMessage(), e);
        }
    }

    @Override
    public void setLightStatusByWorStaCode(String workstationCode, int lightStatus) {
        logger.info("开始设置工作站照明状态 - 工作站代码: {}, 照明状态: {}", workstationCode, lightStatus);
        try {
            String url = "http://" + IP + "/light?status=" + lightStatus;
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

            if (response.getStatusCode() == HttpStatus.OK) {
                WorkstationInfo workstationInfo = workstationInfoMapper.selectByCode(workstationCode);
                workstationInfo.setLightStatus(lightStatus);
                workstationInfo.setModifyTime(LocalDateTime.now());
                if (workstationInfo.getPowerStatus() == 0) {
                    workstationInfo.setPowerStatus(1);
                }
                workstationInfoMapper.updateById(workstationInfo.getId(), workstationInfo);
                logger.info("设置工作站照明状态成功 - 工作站代码: {}, 响应: {}", workstationCode, response.getBody());
            } else {
                logger.error("设置工作站照明状态失败 - 工作站代码: {}, HTTP状态码: {}",
                        workstationCode, response.getStatusCode());
            }
        } catch (Exception e) {
            logger.error("设置工作站照明状态异常 - 工作站代码: {}, 错误信息: {}",
                    workstationCode, e.getMessage(), e);
        }
    }
}